<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import userForm from './userForm.vue';
  import table2excel from 'js-table2excel';
  import Pagination from "@/components/Pagination/index.vue";
  import serachUserid from '@/components/serachUserid/index.vue';
  const userFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const poolOptions = [
    {type:2,label:'亏损池'},
    {type:3,label:'盈利池'},
  ]
  const poolIfDefault = [
    {type:1,label:'是'},
    {type:2,label:'否'},
  ]
  const listQuery = reactive({
    page:1,
    limit:20,
    user_id:'',
    in_pool:''
  })
  const handleSerach = () => {
    getList();
  };
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const res = await luckDrawApi.wishWheelPoolUserList({
      ...listQuery,
      limit:action.total
    })
    loading.close()
    const { list=[] } = res?.data || {}
    const data = list.map((item:any) => {
      return {
        ...item,
        pool_type_text:poolOptions.find(res=>res.type == item.pool_type)?.label || item.pool_type
      }
    })
    const tHeader = [
      { title: '个人池用户记录ID', key: "id"},
      { title: '用户ID', key: "user_id"},
      { title: '用户UUID', key: "user_uuid"},
      { title: '用户昵称', key: "user_nickname"},
      { title: '公会ID', key: "union_id"},
      { title: '公会名称', key: "union_name"},
      { title: '个人池类型', key: "pool_type_text"},
      { title: '礼物池ID', key: "pool_id"},
      { title: '礼物池名称', key: "pool_name"},
      { title: '移除阀值', key: "out_diamond_num"},
      { title: '进入礼物池操作人', key: "admin_name"},
      { title: '进入时间', key: "add_time"},
      { title: '移除礼物池操作人', key: "deladmin_name"},
      { title: '移除时间', key: "del_time"},
    ]
    table2excel(
      tHeader,
      data,
      `许愿转盘-个人礼物池用户`)
  }
  const handleDel = (row:any = {})=>{
    const { nickname='', id=0 } = row
    ElMessageBox.confirm(`确定删除个人礼物池用户：${nickname}吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await luckDrawApi.wishWheelDelPoolUser({
        id,
      });
      loading.close();
      ToastSuccess('删除成功');
      getList();
    })
    .catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await luckDrawApi.wishWheelPoolUserList(listQuery)
    loading.close()
    const { list=[], total=0 } = res?.data || {}
    action.list = list
    action.total = total
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <div class="labelItem">
          <div class="label">是否在礼物池：</div>
          <el-select v-model="listQuery.in_pool" placeholder="请选择是否在礼物池">
            <el-option label="全部" :value="''"/>
            <el-option
              v-for="(item, index) in poolIfDefault"
              :key="index"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="primary" v-rule="'turntable_AlonePoolUserAdd'" @click="userFormRef.openDialog()">新增用户</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="个人池用户记录ID" />
      <el-table-column fixed="left" min-width="80" prop="user_id" label="用户ID" />
      <el-table-column fixed="left" min-width="80" prop="user_uuid" label="用户UUID" />
      <el-table-column fixed="left" min-width="120" show-overflow-tooltip prop="user_nickname" label="用户昵称" />
      <el-table-column min-width="80" prop="union_id" label="公会ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="union_name" label="公会名称" />
      <el-table-column width="140" prop="pool_type" label="个人池类型">
        <template #default="scoped">
          {{ poolOptions.find(res=>res.type == scoped.row.pool_type)?.label || scoped.row.pool_type }}
        </template>
      </el-table-column>
      <el-table-column width="80" prop="pool_id" label="礼物池ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="pool_name" label="礼物池名称" />
      <el-table-column min-width="120" prop="out_diamond_num" label="移除阀值" />
      <el-table-column min-width="130" show-overflow-tooltip prop="admin_name" label="进入礼物池操作人" />
      <el-table-column min-width="180" prop="add_time" label="进入时间" />
      <el-table-column min-width="130" show-overflow-tooltip prop="deladmin_name" label="移除礼物池操作人" />
      <el-table-column min-width="180" prop="del_time" label="移除时间" />
      <el-table-column width="120" fixed="right" label="操作">
        <template #default="scoped">
          <el-button v-if="!scoped.row.deladmin_name" type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <userForm ref="userFormRef" @update-list="getList" />
  </div>
</template>
